<?php
namespace Wap\Controller;
 
class IndexController extends CommonController {
	public function index() {
		$cache = 'home_adv';
		$str = 'adv_id>%d and is_allow=%d and adv_type=%d';
		$data= array(0,1,11);
		$adlist_a = _getList($this -> tb_adv,$str,$data,$cache,'slide_sort');
		$this->assign('adlist_a',$adlist_a);
		
		$num = 6;
		if(IS_POST) {
				$peg = I('post.peg');
				$result = M($this -> tm_product)->where('id>%d and goods_state = 1 and goods_verify = 1',array(0))->order('id desc')->page($peg.','.$num)->select();
				
				if(empty($result)) {
					echo json_encode(0);
				} else if($peg > 10) {
					echo json_encode(0);
				} else {
					echo json_encode($result);
				}
			exit;
		}
		$goods = _getList($this -> tm_product, array(), 'goods_state = 1 and goods_verify = 1', '', 'id desc', 1, $num);

		$order = 'gc_id desc';	
		$where = array(				
			'gc_show' => 1,
		);
		//关联查询
		$goods_list = D('goods_class')->relation(true)->where($where)->order($order)->select();
		
		$f = 0 ;
		foreach ($goods_list as $key => $v) {
			if ($v['goods']) {
				$f = $f + 1 ;
				$goods_list[$key]['floor'] = $f ;				
			} else {
				unset($goods_list[$key]);
			}
		}

		$this->assign('goods',$goods);
		$this->assign('goods_list',$goods_list);
		$this -> display();
	}

	/**
	 * 删除收藏
	 */
	public function delete_collect() {
		if(!isset($_SESSION['boboid']) && !isset($_SESSION['user_id'])) {
			echo json_encode(array('code' => 2));
			exit;
		}
		if(IS_POST) {
			$id = I('post.id');
			if(M('collect')-> delete($id)) {
				echo json_encode(array('code' => 0));
			} else {
				echo json_encode(array('code' => 1, 'message' => '删除失败'));
			};
		}
		exit;
	}
	/**
	 * 添加收藏
	 */
	public function add_collect() {
		if(!isset($_SESSION['boboid']) && !isset($_SESSION['user_id'])) {
			echo json_encode(array('code' => 2));
			exit;
		}
		if(IS_POST) {
			$data = I('post.');
			$data['userid'] = $_SESSION['user_id'];
			if(M('collect')-> add($data)) {
				echo json_encode(array('code' => 0));
			} else {
				echo json_encode(array('code' => 1, 'message' => '收藏失败'));
			};
		}
		exit;
	}
}